package hzjbbis.commons.maintenance.action;

import hzjbbis.framework.context.ContextFacade;
import hzjbbis.framework.context.UserContext;
import hzjbbis.framework.tree.Tree;
import hzjbbis.framework.tree.creator.TreeManager;
import hzjbbis.framework.tree.creator.TreeManagerFactory;
import hzjbbis.framework.util.Tools;
import hzjbbis.framework.xml.dom.extended.DocumentFactory;
import hzjbbis.framework.xml.dom.extended.ExtendedDocument;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.w3c.dom.Element;

/**
 * @version 1.0
 * @author lix
 */
public class InitTempletDocAction extends Action {

    private final String RETURNABLE_NAME = "RETURNABLE";

    private final String TOPSELECTED_NAME = "TOPSELECTED";

    private final String AVAILABLE_NAME = "AVAILABLE";

    private final String SYSTEMFLAG_NAME = "SYSTEMFLAG";

    private final String TITLE_NAME = "TITLE";

    private final String RETURNABLE_VALUE_TRUE = "1";

    private final String TOPSELECTED_VALUE_FALSE = "1";

    private final String AVAILABLE_VALUE_FALSE = "0";

    private final String SYSTEMFLAG_VALUE_FALSE = "0";

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        UserContext userContext = (UserContext) ContextFacade.getUserContext(request);

        String templet = request.getParameter("templet");

        ExtendedDocument doc = DocumentFactory.createExtendedDoc(userContext.getServerContext().getServerFile(templet));

        String treeSourceFile = doc.getDocumentElement().getAttribute("treeSourceFile");

        Element ele = doc.getDocumentElement();
      
        TreeManager treeManager = TreeManagerFactory.getTreeManager(ele.getAttribute("treeName"), userContext);

        Tools.setRequestParams(request, treeManager);

        Tree tree = treeManager.createTree();

        tree.setTitle(treeManager.getOutParamValue(TITLE_NAME));

        tree.getRoot().setAttribute("canselected", treeManager.getOutParamValue(TOPSELECTED_NAME));

        request.setAttribute("templet", templet);
        request.setAttribute("treeSourceFile", treeSourceFile);

        request.setAttribute("tree", tree);
        return mapping.findForward("init");

    }

  /*  private void setRequestParams(Element ele, TreeManager treeManager) {
        InputParams requestParamsInputParams = treeManager.getRequestParams();
        Iterator i = requestParamsInputParams.iterator();
        while (i.hasNext()) {

            InputParam p = (InputParam) i.next();

            if (RETURNABLE_NAME.equals(p.getName())) {
                treeManager.setRequestParam(p.getName(), RETURNABLE_VALUE_TRUE, p.getTypeName());
            } else if (TOPSELECTED_NAME.equals(p.getName())) {
                treeManager.setRequestParam(p.getName(), TOPSELECTED_VALUE_FALSE, p.getTypeName());
            } else if (AVAILABLE_NAME.equals(p.getName())) {
                treeManager.setRequestParam(p.getName(), AVAILABLE_VALUE_FALSE, p.getTypeName());
            } else if (SYSTEMFLAG_NAME.equals(p.getName())) {
                treeManager.setRequestParam(p.getName(), SYSTEMFLAG_VALUE_FALSE, p.getTypeName());
            }
        }
    }*/

}